package com.leonardo.beanme.metadatahandlers;

import java.util.Vector;

public class FormMetadataContainer {
	
	public static final int EXCLUSIVE = 0;
	public static final int ADDITIONAL = 1;
	
	private static final int initialCapacity = 10;
	
	private Vector fieldsConf = new Vector(initialCapacity);
	private int count = 0;
	private final String id;
	private final int type;
	
	public FormMetadataContainer(String id, int type) {
		this.id = id;
		this.type = type;
	}
	
	public boolean hasMoreFields() {
		return count < fieldsConf.size();
	}
	
	public FieldMetadataContainer next() {
		return (FieldMetadataContainer) fieldsConf.elementAt(count++);
	}
	
	public void reset() {
		count = 0;
	}
	
	public void addField(FieldMetadataContainer field) {
		fieldsConf.addElement(field);
	}
	
	public FieldMetadataContainer getFieldOrNew(String fieldName) {
		reset();
		while(hasMoreFields()) {
			FieldMetadataContainer field = next();
			if(fieldName.equals(field.getName()))
				return field;
		}
		if(isExclusive())
			return null;
		FieldMetadataContainer newField = new FieldMetadataContainer();
		addField(newField);
		return newField;
	}
		
	public String getId() {
		return id;
	}
	
	public int getType() {
		return type;
	}
	
	public boolean isExclusive() {
		return type == EXCLUSIVE;
	}
}
